package com.ccut;

import static com.ccut.Constant.ShapeType.TRIANGLE;

public class Trangle extends MyShape {

    private double a;
    private double b;
    private double c;

    public Trangle() {
        super(TRIANGLE);
    }

    public Trangle(double a, double b, double c) {
        this();
        // 修正三角形存在条件判断
        if (a + b <= c || a + c <= b || b + c <= a) {
            System.out.println("三角形不存在");
            return;
        }
        this.a = a;
        this.b = b;
        this.c = c;
        setPerimeter(calculatePerimeter());
        setArea(calculateArea());
        addShape(this); // 添加到形状列表
    }

    public Trangle(double a, double b) {
        this();
        if (a + b <= Math.abs(a - b)) {
            System.out.println("三角形不存在");
            return;
        }
        this.a = a;
        this.b = b;
        this.c = Math.abs(a - b) < a + b ? Math.abs(a - b) + 0.1 : a + b - 0.1; // 简化处理
        setPerimeter(calculatePerimeter());
        setArea(calculateArea());
        addShape(this); // 添加到形状列表
    }

    public Trangle(double a) {
        this();
        if (a <= 0) {  // 修正条件判断
            System.out.println("三角形不存在");
            return;
        }
        this.a = this.b = this.c = a;
        setPerimeter(calculatePerimeter());
        setArea(calculateArea());
        addShape(this); // 添加到形状列表
    }

    @Override
    public double calculatePerimeter() {
        return a + b + c;
    }

    @Override
    public double calculateArea() {
        double p = calculatePerimeter() / 2;
        return Math.sqrt(p * (p - a) * (p - b) * (p - c));
    }

    @Override
    public String toString() {
        return "图形的类型:"+ getShapeType().getChineseName() + "\n" +
                "图形的三边长分别为:"+ a + ", " + b + ", " + c + "\n" +
                "图形的周长(近似):"+ String.format("%.2f", getPerimeter()) + "\n" +
                "图形的面积(近似):"+ String.format("%.2f", getArea());
    }

    @Override
    public void showShape() {
        javax.swing.SwingUtilities.invokeLater(() -> {
            javax.swing.JFrame frame = new javax.swing.JFrame("三角形绘制");
            frame.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);

            javax.swing.JPanel panel = new javax.swing.JPanel() {
                @Override
                protected void paintComponent(java.awt.Graphics g) {
                    super.paintComponent(g);
                    java.awt.Graphics2D g2d = (java.awt.Graphics2D) g;

                    // 设置抗锯齿
                    g2d.setRenderingHint(
                            java.awt.RenderingHints.KEY_ANTIALIASING,
                            java.awt.RenderingHints.VALUE_ANTIALIAS_ON
                    );

                    // 设置支持中文的字体
                    java.awt.Font font = new java.awt.Font("微软雅黑", java.awt.Font.PLAIN, 12);
                    g2d.setFont(font);

                    int width = getWidth();
                    int height = getHeight();

                    // 计算三角形顶点坐标，根据实际边长绘制
                    int[] xPoints = new int[3];
                    int[] yPoints = new int[3];

                    // 将三角形放置在画布中央
                    // 首先确定一个点作为起点
                    xPoints[0] = width / 2;
                    yPoints[0] = height / 2 - 50; // 上方点

                    // 根据边长计算其他两个点
                    // 使用余弦定理计算角度: cos(C) = (a² + b² - c²) / (2ab)
                    double cosC = (a * a + b * b - c * c) / (2 * a * b);
                    // 限制cos值在[-1, 1]范围内，防止计算错误
                    cosC = Math.max(-1, Math.min(1, cosC));

                    double sinC = Math.sqrt(1 - cosC * cosC);

                    // 计算第二个点（基于第一条边a在水平方向）
                    xPoints[1] = xPoints[0] + (int) (a * 20); // 缩放因子20
                    yPoints[1] = yPoints[0];

                    // 计算第三个点（基于第二条边b和夹角C）
                    xPoints[2] = xPoints[0] + (int) (b * 20 * cosC);
                    yPoints[2] = yPoints[0] + (int) (b * 20 * sinC);

                    // 绘制三角形边框
                    g2d.setColor(java.awt.Color.RED);
                    g2d.drawPolygon(xPoints, yPoints, 3);

                    // 填充三角形
                    g2d.setColor(new java.awt.Color(255, 0, 0, 50));
                    g2d.fillPolygon(xPoints, yPoints, 3);

                    // 绘制信息文本
                    g2d.setColor(java.awt.Color.BLACK);
                    g2d.drawString("三角形 (边长: " + String.format("%.2f", a) + ", " +
                            String.format("%.2f", b) + ", " + String.format("%.2f", c) + ")", 20, 20);
                    g2d.drawString("周长: " + String.format("%.2f", getPerimeter()), 20, 40);
                    g2d.drawString("面积: " + String.format("%.2f", getArea()), 20, 60);
                }
            };

            frame.add(panel);
            frame.setSize(400, 400);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}
